#include <iostream>
#include <stdlib.h>
#include "mvmult.hpp"

#define DEBUG 0

using namespace std;

void usage(void);

/** main **/
int main(int argc, char** argv){
	int threads = 0;
	int matrSize = 0;
	int retVal = 0;
	int iterations = 1;
	int datatype = 0;
	unsigned long runtime = 0;
	
	timeVal start,end;

	if (argc != 4 && argc != 5 ){
		usage();
		retVal = -1;
	}

	if (retVal == 0){
		matrSize = atoi(argv[1]);
		threads = atoi(argv[2]);
		datatype = atoi(argv[3]);

		if(argc == 5){
			iterations = atoi(argv[4]);
		}
		
		if(datatype == 0){
		cout << "#Float. Param: Threads " << threads << " Iterations " iterations << endl;
		
		for(int i = 0; i < iterations; i++){
			Vector<float> vect(matrSize);
			Vector<float> erg(matrSize);
			Matrix<float> matr(matrSize, threads);

			vect.init();

			//gettimeofday(&start, NULL);
			erg = matr * vect;
			//gettimeofday(&end, NULL);

#if DEBUG == 1

			cout << "Matrix:" << endl << matr << endl;
			cout << "Vector:" << endl << vect << endl;
			cout << "Result:" << endl << erg << endl;
			cout << endl;
#endif		
			//runtime = calcRuntime(start,end);
			//cout << "0 Runtime(float): " << runtime << " [us] " << runtime/1e6 << " [sec] Size: " << matrSize << " Threads: " << threads << " Iterations: " << iterations << endl;
		}
	
		}else if(datatype == 1){
		cout << "#Double. Param: Threads " << threads << " Iterations " iterations << endl;
		for(int i = 0; i < iterations; i++){
			Vector<double> vect(matrSize);
			Vector<double> erg(matrSize);
			Matrix<double> matr(matrSize, threads);

			vect.init();

			//gettimeofday(&start, NULL);
			erg = matr * vect;
			//gettimeofday(&end, NULL);
#if DEBUG == 1
			cout << "Matrix:" << endl << matr << endl;
			cout << "Vector:" << endl << vect << endl;
			cout << "Result:" << endl << erg << endl;
			cout << endl;
#endif		
			//runtime = calcRuntime(start,end);
			//cout << "1 Runtime(double): " << runtime << " [us] " << runtime/1e6 << " [sec] Size: " << matrSize << " Threads: " << threads << " Iterations: " << iterations << endl;
		}
		
		}

	}

	return retVal;
}/*End of main()*/

/* usage() */
void usage(void){

	cout << "Syntax error: " << endl;
	cout << "./mvmult <Matrix Size> <Threads> <Datatype [0 == float || 1 == double] > <Iterations[opt]>" << endl;

}


